<template>
  <div class="register">
    <div class="bgp"></div>
    <div class="register_container">
      <h1>小滴课程管理系统</h1>
      <el-form
        class="register_form"
        :model="userInfo"
        :rules="rules"
        @keyup.enter="onRegister"
        ref="ref_form"
      >
        <el-form-item prop="userName">
          <el-input placeholder="请输入用户名" v-model.trim="userInfo.userName">
            <template #prefix>
              <el-icon><Avatar /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            show-password
            v-model.trim="userInfo.password"
          >
            <template #prefix>
              <el-icon><Key /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-button type="primary" class="register_submit" @click="onRegister"
          >注册</el-button
        >
        <div class="register_register" @click="toGo">已有账号去登录</div>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { reactive, ref } from "vue-demi";
import router from "../router/index";
import { getRegister } from "../api/index";
/**
 * 初始的ref（用于登录时的提醒，如用户没有输入用户名密码，点击登录应该跳出提示）
 */
const ref_form = ref(null);

/**
 * 表单数据声明
 */
const userInfo = reactive({
  userName: "",
  password: "",
});

/**
 * 表单数据校验规则
 */
const rules = {
  userName: [{ required: "true", trigger: "blur", message: "用户名不能为空" }],
  password: [{ required: "true", trigger: "blur", message: "密码不能为空" }],
};

/**
 * 注册的方法
 */
const onRegister = () => {
  ref_form.value.validate((val) => {
    if (val) {
      getRegisterData();
    }
  });
};

/**
 * 注册的接口
 */
const getRegisterData = async () => {
  //接口的调用
  const res = await getRegister({ userName: userInfo.userName, password: userInfo.password })
  console.log(res)
  // 如果注册成功
  if (res?.message) {
    ElMessage({
      message: '注册成功！！！',
      type: 'success',
    })
    router.push('/login')
  }
}

/**
 * 跳转至登录页面
 */
const toGo = () => {
  router.push("./login");
};
</script>

<style lang='less' scoped>
@-webkit-keyframes fadenum {
  0% {
    opacity: 0;
  }

  100% {
    opacity: 0.85;
  }
}

.register {
  height: 100%;

  .bgp {
    height: 100%;
    background-image: url("../assets/loginBg.jpg");
    background-repeat: no-repeat;
    background-size: 100%;
    background-position: 50% 50%;
  }

  .el-icon {
    height: 30px !important;
  }

  .register_container {
    animation: fadenum 2s ease;
    position: absolute;
    z-index: 1;
    width: 400px;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: all 1s;
    min-height: 263px;
    text-align: center;
    background-color: rgb(212, 241, 249);
    border-radius: 10px;
    padding: 20px;
    opacity: 0.85;

    h1 {
      font-size: 24px;
      transition: all 1s;
      font-weight: bold;
      margin-bottom: 36px;
    }

    .el-card {
      border-radius: 20px;
    }

    .register_form {
      .register_submit {
        width: 100%;
        height: 50px;
        border-radius: 20px;
        margin-bottom: 10px;
      }

      .register_register {
        width: 6.25rem;
        font-size: 0.875rem;
        margin: 0 auto;
        cursor: pointer;
      }

      .register_register:hover {
        color: red;
      }

      :deep(.el-input-group__prepend) {
        padding: 0 10px;
      }
    }
  }
}
</style>